using SqlSugar;using TradeFlow.Server.Models;

namespace TradeFlow.Server.Service
{
    /// <summary>
    /// 数据库初始化服务
    /// </summary>
    public class JxDbInitService : IApplicationService
    {
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public JxDbInitService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 初始化所有表
        /// </summary>
        public void InitTables()
        {
            // 创建所有实体对应的表
            _db.CodeFirst.InitTables(
                typeof(JxGoods),
                typeof(JxPartner),
                typeof(JxBillHead),
                typeof(JxBillList),
                typeof(JxAccountHead),
                typeof(JxAccountList)
            );
        }

        /// <summary>
        /// 检查表是否存在
        /// </summary>
        /// <returns></returns>
        public bool CheckTablesExist()
        {
            return _db.DbMaintenance.IsAnyTable(nameof(JxGoods), false) &&
                   _db.DbMaintenance.IsAnyTable(nameof(JxPartner), false) &&
                   _db.DbMaintenance.IsAnyTable(nameof(JxBillHead), false) &&
                   _db.DbMaintenance.IsAnyTable(nameof(JxBillList), false);
        }
    }
}